#!/usr/bin/env python
# @Software: PyCharm
# @Time : 2022/9/14 16:24
# @Author : v_nahuachen
# @Version：V 0.1
# @File : element_test.py
# @desc :
import minium
from cases.base.base_case import BaseCase
from cases.pages import home_page
from cases.pages import base_page


@minium.ddt_class()
class ElementTest(BaseCase):

    def setUp(self) -> None:
        super().setUp()
        # 页面跳转
        self.HomePage.interface_page("element")

    def __init__(self, methodName='runTest'):
        """
        初始化Element实例类
        """
        super(ElementTest, self).__init__(methodName)
        self.HomePage = home_page.HomePage(self)
        self.BasePage = base_page.BasePage(self)

    def test_move(self):
        """
        movable-view 容器拖拽滑动
        """
        ele = self.BasePage.get_element_selector("movable-view")
        # reset 重置
        ele.move_to(0, 0)  # 把movable-view复位
        self.page.wait_for(2)
        rect = ele.rect  # 记录初始位置
        self.logger.warn(rect)
        ele.move_to(20, 100)  # 移动到坐标为20, 100的地方
        self.page.wait_for(2)
        self.assertDictContainsSubset(
            {
                "left": 20 + rect.x,
                "top": 100 + rect.y,
            },
            ele.rect,
        )

    def test_scroll_to(self):
        """
        测试 scroll-view容器滚动
        :return:
        """
        # hook监听scroll事件
        # 横向滚动20像素, value=20,param="x"
        callback_called, callback_args = self.BasePage.hook_current_page_method(
            method="scroll",
            selector="scroll-view",
            attr_method="scroll_to",
            value=20,
            param="x")
        self.assertTrue(callback_called, "callback called")
        self.assertEqual(callback_args["detail"]["scrollLeft"], 20, "scroll ok")

    def test_swipe_to(self):
        """
        测试swiper容器
        :return:
        """
        # hook监听swiperChange事件
        # value=2 切换到第二个tab
        callback_called, callback_args = self.BasePage.hook_current_page_method(
            method="swiperChange",
            selector="swiper",
            attr_method="swipe_to",
            value=2)

        self.assertTrue(callback_called, "callback called")
        self.assertEqual(callback_args["detail"]["current"], 2, "swipe ok")

    @minium.ddt_unpack
    @minium.ddt_case(
        (0, 0, "bindPickerChange"),
        (1, "17:30", "bindTimeChange"),
        (2, "2022-9-15", "bindDateChange"),
    )
    def test_pick(self, index, value, method):
        """
        测试 picker选择器
        不会进行真正的滚动选择操作, 通过派发change事件来模拟选择完成操作
        :param index:
        :param value:
        :param method:
        :return:
        """
        # hook监听pick选择事件
        callback_called, callback_args = self.BasePage.hook_current_pick_method(
            method=method,
            selector="picker",
            attr_method="pick",
            index=index,
            value=value)
        self.assertTrue(callback_called, "callback called")
        self.assertEqual(callback_args["detail"]["value"], value, "pick ok")

    def test_input(self):
        """
        测试输入:
           1. 在`input`组件输入`test`
           2. 通过hook绑定的输入事件获取输入的详
        :return:
        """
        callback_called, callback_args = self.BasePage.hook_current_page_method(
            method="userNameInput",
            selector="#username",
            attr_method="input",
            value="test")

        # 检验有输入事件触发
        self.assertTrue(callback_called, "callback called")
        # 检验输入详情
        self.assertEqual(callback_args["detail"]["value"], "test", "input text ok")